package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsStorage;

import com.qf.service.DtsAdminStorageService;
import com.qf.util.ResponseUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/storage")
public class DtsAdminStorageController {

    @Autowired
    DtsAdminStorageService storageService;

    @RequestMapping("/update")
    public Object update(@RequestBody DtsStorage storage){
        storageService.update(storage);
        return ResponseUtil.ok();
    }
    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
        storageService.delete(storage);
        return ResponseUtil.ok();
    }


    @RequestMapping("/create")
    public Object create(MultipartFile file) throws Exception {
        byte[] bytes = file.getBytes();
        String s = UUID.randomUUID().toString();
        String originalFilename = file.getOriginalFilename();
        String key = s + originalFilename;
        DtsStorage storage = storageService.add(bytes, key,originalFilename,(int)file.getSize(), file.getContentType()  );
        return ResponseUtil.ok(storage);
    }

    @RequestMapping("/list")
    public Object list(
            @RequestParam(name = "page", required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit", required = false,defaultValue = "10") Integer limit,
            String sort, String order ,String key ,String name

    )
    {
        IPage<DtsStorage> ipage = storageService.queryPage(page, limit, sort, order,key ,name);
        Map<String,Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());

        return ResponseUtil.ok(data);
    }
}
